package service;

import com.google.gson.Gson;
import config.RPC;
import entity.Transaction;

@RPC
public class JsonReaderServiceImpl implements JsonReaderService{
    @Override
    public String readString(String input) {
        String[] parts=input.split(" ");
        Gson gson = new Gson();
        if (parts.length == 3) {
            try {
                String type = parts[0];
                String cardNumber = parts[1];
                double amount = Double.parseDouble(parts[2]);

                Transaction transaction = new Transaction();
                transaction.setType(type);
                transaction.setCardNumber(cardNumber);
                transaction.setAmount(amount);
                //转为json字符流
                String json = gson.toJson(transaction);
                return json;
            } catch (NumberFormatException e) {
                System.out.println("金额需要是数字，请重新输入");
                return null;
            }
        } else {
            System.out.println("输入格式不正确，请按照'类型 卡号 金额'的格式输入");
            return null;
        }
    }
}
